#include "gamescene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include "dataconfig.h"
#include <QIcon>
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
#include <QPropertyAnimation>

GameScene::GameScene(int level, QWidget *parent)
    : QMainWindow(parent),
      m_level(level),
      m_lblWinGame(nullptr)
{
    this->setWindowTitle(tr("金币游戏"));
    this->setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    this->setWindowIcon(QIcon(IMG_GAME_ICON));

    //menu
    QMenuBar *lvMenuBar = this->menuBar();
    QMenu *menuFile = lvMenuBar->addMenu(tr("文件"));
    QAction *acBack = menuFile->addAction(tr("返回"));
    connect(acBack, &QAction::triggered, this, [=](){
        //返回关卡界面
        GameController::getInstance()->deleteGameScene();
        GameController::getInstance()->showLevelScene();
    });
    QAction *acQuit = menuFile->addAction(tr("退出"));
    connect(acQuit, &QAction::triggered, this, [=](){
        exit(0);
    });

    //游戏胜利标志
    m_lblWinGame = new QLabel(this);
    QPixmap winPixmap(IMG_LEVEL_COMPLETE);
    m_lblWinGame->setPixmap(winPixmap);
    m_lblWinGame->setGeometry(0, 0, winPixmap.width(), winPixmap.height());
    m_lblWinGame->move((GAME_WIDTH - m_lblWinGame->width()) / 2, -m_lblWinGame->height());

    //游戏区
    DataConfig gameDataInfo;
    for (int i=0; i<4; i++)
    {
        for (int j=0; j<4; j++)
        {
            //
            m_gameInfoArray[i][j] = gameDataInfo.mData[m_level][i][j];
            //游戏背景
            QLabel *lblBgCoin = new QLabel(this);
            QPixmap bgCoinPix(IMG_BODER_NODE);
            lblBgCoin->setPixmap(bgCoinPix);
            lblBgCoin->setGeometry(0, 0, bgCoinPix.width(), bgCoinPix.height());
            lblBgCoin->move(95 + i * 50, 200 + j * 50);
            //金币
            QString strCoin = "";
            if (m_gameInfoArray[i][j] == 1)
            {
                strCoin = ":/res/Coin0001.png";
            }
            else
            {
                strCoin = ":/res/Coin0008.png";
            }
            m_btnCoin[i][j] = new GameCoin(strCoin, lblBgCoin);
            m_btnCoin[i][j]->move(2, 4);
            m_btnCoin[i][j]->setPosX(i);
            m_btnCoin[i][j]->setPosY(j);
            m_btnCoin[i][j]->setFlag(m_gameInfoArray[i][j]);
            connect(m_btnCoin[i][j], &QPushButton::clicked, this, [=](){
                //当前金币改变状态
                m_gameInfoArray[i][j] = m_gameInfoArray[i][j] == 1 ? 0 : 1;
                m_btnCoin[i][j]->changeFlag();
                GameController::getInstance()->playFlipCoinSound();
                QTimer::singleShot(30, this, [=](){
                    //位置
                    int xIndex = m_btnCoin[i][j]->posX();
                    int yIndex = m_btnCoin[i][j]->posY();
                    //上面
                    if (xIndex > 0)
                    {
                        m_gameInfoArray[xIndex - 1][yIndex] = m_gameInfoArray[xIndex - 1][yIndex] == 1 ? 0 : 1;
                        m_btnCoin[xIndex - 1][yIndex]->changeFlag();
                    }
                    //下面
                    if (xIndex < 3)
                    {
                        m_gameInfoArray[xIndex + 1][yIndex] = m_gameInfoArray[xIndex + 1][yIndex] == 1 ? 0 : 1;
                        m_btnCoin[xIndex + 1][yIndex]->changeFlag();
                    }
                    //左边
                    if (yIndex > 0)
                    {
                        m_gameInfoArray[xIndex][yIndex - 1] = m_gameInfoArray[xIndex][yIndex - 1] == 1 ? 0 : 1;
                        m_btnCoin[xIndex][yIndex - 1]->changeFlag();
                    }
                    //右边
                    if (yIndex < 3)
                    {
                        m_gameInfoArray[xIndex][yIndex + 1] = m_gameInfoArray[xIndex][yIndex + 1] == 1 ? 0 : 1;
                        m_btnCoin[xIndex][yIndex + 1]->changeFlag();
                    }

                    //判断胜负
                    bool isWin = true;
                    for (int m=0; m<4; m++)
                    {
                        for (int n=0; n<4; n++)
                        {
                            //
                            if(m_gameInfoArray[m][n] == 0)
                            {
                                isWin = false;
                                break;
                            }
                        }
                    }
                    if(isWin == true)
                    {
                        qDebug() << "恭喜你，游戏胜利";
                        GameController::getInstance()->playWinSound();
                        //胜利动画
                        QPropertyAnimation *winAnimation = new QPropertyAnimation(m_lblWinGame, "geometry");
                        winAnimation->setDuration(1000);
                        winAnimation->setStartValue(QRect(m_lblWinGame->x(), m_lblWinGame->y(),
                                                          m_lblWinGame->width(), m_lblWinGame->height()));
                        winAnimation->setEndValue(QRect(m_lblWinGame->x(), 40,
                                                        m_lblWinGame->width(), m_lblWinGame->height()));
                        winAnimation->setEasingCurve(QEasingCurve::OutBounce);
                        winAnimation->start();
                        //设置金币不可点击
                        for (int m=0; m<4; m++)
                        {
                            for (int n=0; n<4; n++)
                            {
                                m_btnCoin[m][n]->setIsWin(true);
                            }
                        }
                    }
                });
            });

        }
    }

    //等级信息
    QLabel *lblLevel = new QLabel(this);
    lblLevel->setText(QString("LEVEL-%1").arg(m_level));
    lblLevel->setGeometry(10, GAME_HEIGHT - 80, 180, 50);
    QFont levelFont;
    levelFont.setFamily("华文新魏");
    levelFont.setBold(true);
    levelFont.setPointSize(20);
    lblLevel->setFont(levelFont);

    //返回按钮
    GamePushButton *btnBack = new GamePushButton(BTN_BACK_NORMAL, BTN_BACK_PRESSED);
    btnBack->setParent(this);
    btnBack->move(GAME_WIDTH - btnBack->width(), GAME_HEIGHT - btnBack->height());
    connect(btnBack, &QPushButton::clicked, this, [=](){
        //返回游戏界面
        GameController::getInstance()->deleteGameScene();
        GameController::getInstance()->showLevelScene();
    });

}

void GameScene::paintEvent(QPaintEvent *event)
{
    QPainter gamePainter(this);
    QPixmap gamePix;
    bool retBg = gamePix.load(IMG_BG_GAMESCENE);
    if (retBg)
    {
        gamePainter.drawPixmap(0, 0, gamePix);
    }
    //
    bool retTitle = gamePix.load(IMG_GAME_TITLE);
    if (retTitle)
    {
        gamePix = gamePix.scaled(gamePix.width() * 0.7, gamePix.height() * 0.7);
        gamePainter.drawPixmap(10, 50, gamePix);
    }
}
